package cn.stylefeng.guns.modular;

import cn.stylefeng.guns.modular.info.service.CpSGService;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * @program: football_manage
 * @description:
 * @author: xiesm
 * @create: 2021-10-27 17:23
 **/
@Slf4j
@RestController
@RequestMapping("/gunsApi/auth")
public class TestController {
    @Autowired
    private CpSGService cpSGService;

    @RequestMapping("/testCpCheck")
    public String testCpCheck() throws Exception {
        cpSGService.check();
        return "ok";
    }

    @RequestMapping("/testCpCheck2")
    public String testCpCheck2() throws Exception {
        cpSGService.check2();
        return "ok";
    }

    @RequestMapping("/repeatWarn")
    public String repeatWarn() throws Exception {
        cpSGService.repeatWarn();
        return "ok";
    }

    @RequestMapping("/repeatWarn2")
    public String repeatWarn2() throws Exception {
        cpSGService.repeatWarn2();
        return "ok";
    }

    @RequestMapping("/repeatWarn3")
    public String repeatWarn3() throws Exception {
        cpSGService.repeatWarn3();
        return "ok";
    }

    @RequestMapping("/res")
    public ResponseData res(@RequestBody String requestBody) {
        log.info("回调参数：{}", requestBody);
        return ResponseData.success();
    }

    @PostMapping("/quanxun/{productCode}")
    public JSONObject callbackQuanXun(@PathVariable String productCode, @RequestBody String requestBody) {
        JSONObject res = new JSONObject();
        try {
            JSONObject jsonObject = JSONObject.parseObject(requestBody);
            log.info("圈讯 {} 接口回调数据 {}", productCode, jsonObject.toJSONString());
            res.put("code", 0);
            log.info(JSONObject.toJSONString(res));
            return res;
        } catch (Exception exception) {
            log.error("处理圈讯回调异常", exception);
            res.put("code", 1);
            return res;
        }
    }

//    @PostMapping("/dycw/{tpaId}/{reqId}")
//    public Boolean callbackDycw(@PathVariable String tpaId, @PathVariable String reqId,
//                                @RequestBody String requestBody) {
//        try {
//            JSONObject jsonObject = JSONObject.parseObject(requestBody);
//            log.info("第一车网 {},{}, 接口回调数据 {}", tpaId, reqId, jsonObject.toJSONString());
////            return "success";
//            return true;
//        } catch (Exception exception) {
//            log.error("第一车网回调异常", exception);
////            return "error";
//            return false;
//        }
//    }

    @SneakyThrows
    @PostMapping("/crash/{apiId}/{tpaId}/{reqId}")
    public void callbackDycw(@PathVariable String apiId, @PathVariable String tpaId,
                             @PathVariable String reqId,
                             @RequestBody String requestBody, HttpServletResponse response) {
        ServletOutputStream out = response.getOutputStream();
        try {
            JSONObject jsonObject = JSONObject.parseObject(requestBody);
            log.info("第一车网 {},{},{}, 接口回调数据 {}", apiId, tpaId, reqId, jsonObject.toJSONString());
            out.print("success");
        } catch (Exception exception) {
            log.error("第一车网回调异常", exception);
            out.print("error");
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @SneakyThrows
    @PostMapping("/ysha/{apiId}/{tpaId}/{reqId}/{appkey}")
    public void callYSha(@PathVariable String apiId, @PathVariable String tpaId,
                         @PathVariable String reqId,
                         @PathVariable String appkey,
                         String code, String msg, String token,
                         String verifyMsg, String data,
                         HttpServletResponse response) {
        ServletOutputStream out = response.getOutputStream();
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", code);
            jsonObject.put("msg", msg);
            jsonObject.put("token", token);
            jsonObject.put("verifyMsg", verifyMsg);
            jsonObject.put("data", data);
            log.info("一砂返回 {},{},{},{}, 接口回调数据 {}", apiId, tpaId, reqId, appkey, jsonObject.toJSONString());
            out.print("success");
        } catch (Exception exception) {
            log.error("第一车网回调异常", exception);
            out.print("error");
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}